package com.example.farm.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.farm.Bean.UserData;
import com.example.farm.mapper.UserDataMapper;
import com.example.farm.service.UserDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚
 * @since 2023-04-15
 */
@Service
@Transactional
public class UserDataServiceImpl extends ServiceImpl<UserDataMapper, UserData> implements UserDataService {


    @Autowired
    private UserDataMapper userDataMapper;

    @Override
    public int insertUserData(UserData userData) {
        if (userData.getUid() == null) {
            return 0;
        } else {
            UpdateWrapper<UserData> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uid", userData.getUid());
            updateWrapper.set("vip", userData.getVip());
            updateWrapper.set("landPurchased", userData.getLandPurchased());
            updateWrapper.set("ItemPurchased", userData.getItemPurchased());
            return userDataMapper.update(userData, updateWrapper);
        }
    }

    @Override
    public UserData selectUserData(String username) {
        return userDataMapper.selectByUserId(username);
    }

    //注册
    @Override
    public int register(UserData userData) {
        //判断是否已经注册
        if (userDataMapper.selectByUserId(userData.getNickname()) != null) {
            return 0;
        } else {
            userDataMapper.insert(userData);
            return 1;
        }
    }

    //登录
    @Override
    public int login(UserData userData) {
        //判断是否已经注册
        if (userDataMapper.selectByUserId(userData.getNickname()) == null) {
            return 0;
        } else if (!userDataMapper.selectByUserId(userData.getNickname()).getPassword().equals(userData.getPassword())) {
            return 0;
        } else if (userDataMapper.selectByUserId(userData.getNickname()).getPassword().equals(userData.getPassword())) {
            return 1;
        } else {
            return 0;
        }


    }
}